<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *案例管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','cid','sid','title','image','ishot','hdp','remark','status','pay','paixu','createtime');
	$list=pdo_getslice('jy_cmsdecorate_case',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['cname']=pdo_getcolumn('jy_cmsdecorate_company',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['cid']),'title');
			$item['sname']=pdo_getcolumn('jy_cmsdecorate_stylist',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['sid']),'name');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'案例列表'),
			array('op'=>'edit','title'=>'新增案例'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'案例id','type'=>'string'),
			array('name'=>'title','title'=>'案例名称','type'=>'string'),
			array('name'=>'cname','title'=>'所属公司','type'=>'string'),
			array('name'=>'sname','title'=>'所属设计师','type'=>'string'),
			array('name'=>'image','title'=>'案例图','type'=>'image'),
			array('name'=>'pay','title'=>'设计费用','type'=>'string'),
			array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'是否开启','type'=>'switch','set'=>'set'),
		),
	//	'actions'=>array(
	//		array('type'=>'post','op'=>'toutiao','title'=>'推送到头条号','message'=>'确定推送该文章到头条号吗！','show'=>''),
	//		array('type'=>'post','op'=>'wechat','title'=>'推送到企鹅号','message'=>'确定推送该文章到企鹅号吗！','show'=>''),
	//		),
		),
		'search'=>array(
			array('name'=>'title','title'=>'案例名称','type'=>'string','width'=>'15'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cmsdecorate_case',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$item['hdp']=json_decode($item['hdp']);
	$return['item']=$item;
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回案例列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'案例基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'名称','name'=>'title','help'=>'','ismust'=>true),
					array('type'=>'image','title'=>'案例图','name'=>'image','help'=>'','ismust'=>true),
					array('type'=>'select','options'=>'cname','option_title'=>'title','title'=>'所属公司','name'=>'cid','help'=>'','ismust'=>true),
					array('type'=>'select','options'=>'sname','option_title'=>'name','title'=>'所属设计师','name'=>'sid','help'=>'','ismust'=>true),
					array('type'=>'string','title'=>'设计花费','name'=>'pay','help'=>''),
					array('type'=>'string','title'=>'案例说明','name'=>'remark','help'=>''),
					array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
				//	array('type'=>'switch','title'=>'是否首页','name'=>'ishow','help'=>''),
					array('type'=>'switch','title'=>'是否热门','name'=>'ishot','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>''),
				)
			),
			/*	array(
				'title'=>'案例幻灯片',
				'list'=>array(
				array('type'=>'array','name'=>'hdp','add_tip'=>'添加一个案例幻灯片',
				'list'=>array(
					array('type'=>'image','name'=>'image','title'=>'幻灯片'),	
					array('type'=>'string','name'=>'url','title'=>'幻灯片url'),	
				),
				'details'=>array(
					array('type'=>'image','name'=>'image','title'=>'幻灯片'),	
					array('type'=>'string','name'=>'url','title'=>'幻灯片url'),
				)
				),
				)
			),          */
		)
	);
	$return['cname']=pdo_getall('jy_cmsdecorate_company',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['sname']=pdo_getall('jy_cmsdecorate_stylist',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请完善案例名称','error');
	}
	if(empty($postdata['image'])){
		exi('请完善案例图','error');
	}
	if(!$postdata['pay']){
		exi('请完善设计花费','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'remark'=>trim($postdata['remark']),
		'pay'=>intval($postdata['pay']),
		'cid'=>intval($postdata['cid']),
		'sid'=>intval($postdata['sid']),
		'hdp'=>ijson_encode($postdata['hdp']),
		'paixu'=>intval($postdata['paixu']),
		'ishot'=>intval($postdata['ishot']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_cmsdecorate_case',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_cmsdecorate_case',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_cmsdecorate_case',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的案例！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_cmsdecorate_case',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_cmsdecorate_case',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
